<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名称" prop="estateName" v-if="userType < 3">
        <el-input v-model="queryParams.estateName" placeholder="请输入小区名称" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="登记时间" prop="visitTime">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"-->
      <!--                   v-hasPermi="['system:record:add']">新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"-->
      <!--                   v-hasPermi="['system:record:edit']">修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"-->
      <!--                   v-hasPermi="['system:record:remove']">删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['system:record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="物业id" align="center" prop="propertyId" />-->
      <!--      <el-table-column label="物业名称" align="center" prop="propertyName" />-->
      <!--      <el-table-column label="小区id" align="center" prop="estateId" />-->
      <el-table-column label="小区名称" align="center" prop="estateName" />
      <!--      <el-table-column label="访问记录id" align="center" prop="visitId" />-->
      <el-table-column label="访问登记时间" align="center" prop="visitTime" width="180" />
      <!--      <el-table-column label="分组编号" align="center" prop="groupCode" />-->
      <!--      <el-table-column label="场景编号" align="center" prop="sceneCode" />-->
      <el-table-column label="场景名称" align="center" prop="sceneName" />
      <!--      <el-table-column label="终端用户名称" align="center" prop="clientName" />-->
      <el-table-column label="手机号" align="center" prop="mobile" />
      <!--      <el-table-column label="车牌号" align="center" prop="carNum" />-->
<!--      <el-table-column label="姓名" align="center" prop="realName" />-->
      <el-table-column label="昵称" align="center" prop="nickName" />
      <!--      <el-table-column label="证件类型" align="center" prop="certType" />-->
<!--      <el-table-column label="证件编号" align="center" prop="certNo" />-->
      <el-table-column label="访问类型" align="center" prop="visitType" />
      <el-table-column label="表单信息" align="center" prop="formDataContent" />
<!--      <el-table-column label="其他信息" align="center" prop="formDataContent" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:record:edit']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改支付宝门禁登记记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物业名称" prop="propertyName">
              <el-input v-model="form.propertyName" readonly placeholder="请输入物业名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小区名称" prop="estateName">
              <el-input v-model="form.estateName" readonly placeholder="请输入小区名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="访问记录ID" prop="visitId">
              <el-input v-model="form.visitId" readonly placeholder="请输入访问记录id" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="访问登记时间" prop="visitTime">
              <el-date-picker clearable size="mini" v-model="form.visitTime" readonly type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择访问登记时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分组编号" prop="groupCode">
              <el-input v-model="form.groupCode" readonly placeholder="请输入分组编号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场景编号" prop="sceneCode">
              <el-input v-model="form.sceneCode" readonly placeholder="请输入场景编号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场景名称" prop="sceneName">
              <el-input v-model="form.sceneName" readonly placeholder="请输入场景名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="终端用户名称" prop="clientName">
              <el-input v-model="form.clientName" readonly placeholder="请输入终端用户名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="form.mobile" readonly placeholder="请输入手机号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carNum">
              <el-input v-model="form.carNum" readonly placeholder="请输入车牌号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="realName">
              <el-input v-model="form.realName" readonly placeholder="请输入姓名" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="form.nickName"  readonly placeholder="请输入昵称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件编号" prop="certNo">
              <el-input v-model="form.certNo"  readonly placeholder="请输入证件编号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他信息" prop="extInfo">
              <el-input v-model="form.extInfo"  readonly placeholder="请输入其他信息" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="表单信息" prop="formDataContent">
              <el-input v-model="form.formDataContent"  readonly type="textarea" placeholder="请输入内容" size="mini"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/module/estate/visitRecord";

export default {
  name: "Record",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付宝门禁登记记录表格数据
      recordList: [],
      dateRange:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realName: undefined,
        estateName: undefined,
        beginDate:undefined,
        endDate:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      userType:0,
    }
  },
  created() {
    this.getList();
    this.userType = this.$store.state.user.user_type;
  },
  methods: {
    /** 查询支付宝门禁登记记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        propertyName: undefined,
        estateId: undefined,
        estateName: undefined,
        visitId: undefined,
        visitTime: undefined,
        groupCode: undefined,
        sceneCode: undefined,
        sceneName: undefined,
        clientName: undefined,
        mobile: undefined,
        carNum: undefined,
        realName: undefined,
        nickName: undefined,
        certType: undefined,
        certNo: undefined,
        visitType: undefined,
        formData: undefined,
        extInfo: undefined,
        formDataContent: undefined
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log('dateRange',this.dateRange);
      if(this.dateRange !=undefined && this.dateRange.length === 2){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.beginDate = undefined;
      this.queryParams.endDate = undefined;
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付宝门禁登记记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改支付宝门禁登记记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除支付宝门禁登记记录编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有支付宝门禁登记记录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
